package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Color;
import java.awt.GridLayout;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JButton;

import controller.CustomerController;

public class CustomerTab extends JPanel implements ActionListener{

	JButton findButton, createButton, saveButton, clearButton, removeButton;
	JLabel searchLabel, findLabel, nameLabel, addressLabel, postalCodeLabel, cityLabel, phoneLabel, emailLabel,discountLabel, searchException, standardException, orders;
	JTextField searchField, nameField, idField, addressField, postalCodeField, cityField, phoneField, emailField,discountField;
	JPanel row1, row2, row3, row4, row5, row6, row7, row8;
	JSeparator line;
	JScrollPane scrollPane;
	JTable table;
	CustomerTableModel model;
	CustomerController customerController = CustomerController.getInstance();
	
	public CustomerTab(){
		
		setLayout(null);
		
		//Row 1
		row1 = new JPanel();
		row1.setBounds(0, 0, 600, 60);
		row1.setLayout(null);
		add(row1);
		searchLabel = new JLabel("Search");
		searchLabel.setBounds(15, 18, 60, 10);
		searchField = new JTextField(15);
		searchField.setBounds(90, 15, 150, 20);
		findButton = new JButton("Find");
		findButton.setBounds(250, 15, 80, 20);
		findButton.addActionListener(this);
		row1.add(searchLabel);
		row1.add(searchField);
		row1.add(findButton);
		//searchException label
		searchException = new JLabel();
		searchException.setBounds(15, 40, 400, 20);
		searchException.setForeground(Color.red);
		row1.add(searchException);
		
		//Row 2
		row2 = new JPanel();
		row2.setBounds(0, 60, 600, 35);
		row2.setLayout(null);
		add(row2);
		phoneLabel = new JLabel("Phone/ID");
		phoneLabel.setBounds(15, 18, 80, 10);
		phoneField = new JTextField(15);
		phoneField.setBounds(90, 15, 150, 20);
		row2.add(phoneLabel);
		row2.add(phoneField);
		//standardException label
		standardException = new JLabel();
		standardException.setBounds(250, 18, 350, 20);
		standardException.setForeground(Color.red);
		row2.add(standardException);
		
		//Row 3
		row3 = new JPanel();
		row3.setBounds(0, 85, 600, 35);
		row3.setLayout(null);
		add(row3);
		nameLabel = new JLabel("Name");
		nameLabel.setBounds(15, 18, 60, 10);
		nameField = new JTextField(15);
		nameField.setBounds(90, 15, 150, 20);
		row3.add(nameLabel);
		row3.add(nameField);
		discountLabel = new JLabel("Discount");
		discountLabel.setBounds(250, 18, 60, 10);
		row3.add(discountLabel);
		discountField = new JTextField(15);
		discountField.setBounds(310, 15, 150, 20);
		row3.add(discountField);
		
		
		
		
		//Row 4
		row4 = new JPanel();
		row4.setBounds(0, 110, 600, 35);
		row4.setLayout(null);
		add(row4);
		addressLabel = new JLabel("Address");
		addressLabel.setBounds(15, 18, 60, 10);
		addressField = new JTextField(15);
		addressField.setBounds(90, 15, 150, 20);
		row4.add(addressLabel);
		row4.add(addressField);
		
		//Row 5
		row5 = new JPanel();
		row5.setBounds(0, 135, 600, 35);
		row5.setLayout(null);
		add(row5);
		postalCodeLabel = new JLabel("Postal Code");
		postalCodeLabel.setBounds(15, 18, 80, 10);
		postalCodeField = new JTextField(15);
		postalCodeField.setBounds(90, 15, 150, 20);
		cityLabel = new JLabel("City");
		cityLabel.setBounds(250, 15, 60, 20);
		cityField = new JTextField(15);
		cityField.setBounds(310, 15, 150, 20);
		row5.add(postalCodeLabel);
		row5.add(postalCodeField);
		row5.add(cityLabel);
		row5.add(cityField);
		
		//Row 6
		row6 = new JPanel();
		row6.setBounds(0, 160, 600, 35);
		row6.setLayout(null);
		add(row6);
		emailLabel = new JLabel("Email");
		emailLabel.setBounds(15, 18, 80, 10);
		emailField = new JTextField(15);
		emailField.setBounds(90, 15, 150, 20);
		row6.add(emailLabel);
		row6.add(emailField);
		
		//Row 7
		row7 = new JPanel();
		row7.setBounds(0, 195, 600, 80);
		row7.setLayout(null);
		add(row7);
		createButton = new JButton("Create");
		createButton.setBounds(15, 15, 80, 20);
		createButton.addActionListener(this);
		saveButton = new JButton("Save");
		saveButton.setBounds(100, 15, 80, 20);
		saveButton.addActionListener(this);
		clearButton = new JButton("Clear");
		clearButton.setBounds(185, 15, 80, 20);
		clearButton.addActionListener(this);
		removeButton = new JButton("Remove");
		removeButton.setBounds(270, 15, 80, 20);
		removeButton.addActionListener(this);
		row7.add(createButton);
		row7.add(saveButton);
		row7.add(clearButton);
		row7.add(removeButton);
		line = new JSeparator();
		line.setBounds(10,60, 560, 30);
		row7.add(line);
		
		// row 8 
		row8 = new JPanel();
		row8.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black), "Orders"));
		row8.setBounds(10, 280, 560, 200);
		row8.setLayout(new GridLayout(1,0));
		add(row8);
		// insert orders into a table inside a scroll pane
		model = new CustomerTableModel(new ArrayList<String>(),"Order #");
		table = new JTable(model);		
		scrollPane = new JScrollPane(table);
		table.setFillsViewportHeight(true);
		row8.add(scrollPane);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == createButton){
			System.out.println("Creating customer.");
			try{
				clearException();
				createCustomer();
			} catch(Exception ex) {
				standardException();
			}
		}
		if(e.getSource() == saveButton){
			System.out.println("Saving.");
			try{
				clearException();
				updateCustomer();
			} catch(Exception ex) {
				standardException();
			}
		}
		if(e.getSource() == findButton){
			System.out.println("Searching.");
			try{
				clearException();
				findCustomer();
				
			} catch(Exception ex) {
				searchException();
			}
		}
		if(e.getSource() == removeButton){
			System.out.println("Removing.");
			try{
				clearException();
				removeCustomer();
				clearFields();
			} catch(Exception ex) {
				searchException();
			}
		}
		if(e.getSource() == clearButton){
			System.out.println("Clearing.");
			clearFields();
		}
	}
	
	private void removeCustomer(){
		customerController.removeCustomer(Integer.parseInt(phoneField.getText()));
	}
	
	private void createCustomer(){
		customerController.createCustomer(nameField.getText(),addressField.getText(),
										Integer.parseInt(postalCodeField.getText()), cityField.getText(),
										Integer.parseInt(phoneField.getText()), emailField.getText(),Double.parseDouble(discountField.getText()));
	}
	
	private void findCustomer(){
		nameField.setText(customerController.getName(Integer.parseInt(searchField.getText())));
		addressField.setText(customerController.getAdress(Integer.parseInt(searchField.getText())));
		postalCodeField.setText(String.valueOf(customerController.getPostalCode(Integer.parseInt(searchField.getText()))));
		cityField.setText(customerController.getCity(Integer.parseInt(searchField.getText())));
		phoneField.setText(String.valueOf(customerController.getPhone(Integer.parseInt(searchField.getText()))));
		emailField.setText(customerController.getEmail(Integer.parseInt(searchField.getText())));
		discountField.setText(String.valueOf(customerController.getDiscount(Integer.parseInt(searchField.getText()))));
		row8.remove(scrollPane);
		table = new JTable(new CustomerTableModel(CustomerController.getInstance().getCustomerOrders(Integer.parseInt(phoneField.getText())), "Order #"));
		scrollPane = new JScrollPane(table);
		table.setFillsViewportHeight(true);
		row8.add(scrollPane);
	}
	
	private void updateCustomer(){
		customerController.updateCustomer(nameField.getText(),addressField.getText(),
										Integer.parseInt(postalCodeField.getText()), cityField.getText(),
										Integer.parseInt(phoneField.getText()), emailField.getText(),Double.parseDouble(discountField.getText()));
	}
	
	private void clearFields(){
		searchField.setText(null);
		nameField.setText(null); 
		addressField.setText(null);
		postalCodeField.setText(null);
		cityField.setText(null);
		phoneField.setText(null);
		emailField.setText(null);
		discountField.setText(null);
		clearException();
		row8.remove(scrollPane);
		table = new JTable(new CustomerTableModel(new ArrayList<String>(), "Order #"));
		scrollPane = new JScrollPane(table);
		table.setFillsViewportHeight(true);
		row8.add(scrollPane);
		
	}
	
	private void searchException(){
		searchException.setText("Search Error - Try Again!");
	}
	
	private void standardException(){
		standardException.setText("Error! - Please Check The Information.");
	}
	
	private void clearException(){
		searchException.setText(null);
		standardException.setText(null);
	}
}
